<template>
	<view class="cart_lis">
		<van-search v-model="keyword" shape="round" placeholder="请输入产品名称" @search="onSearch" />
		<view class="container">
			<car-list ref="mycar" :carList="carList" @allSelBtn="allSelBtn" @selShop="selShop" @selGoods="selGoods"
				@jsbtn="jsbtn" @delbtn="delbtn" :allSel="allSel" @allSel="allSelClick" @changeNum="changeNum"
				@delThis="delThis" :groupId="group_id"></car-list>

			<van-empty :description="keyword ? '没有找到相关商品' : '购物车空空如也'" v-if="carList.length == 0" />

		</view>
		<view class="likeimages">
			<image src="https://daiyue.gzzhenyi.net/static/index/like.svg" mode="scaleToFill" />
		</view>
		<guess-like />
		<view class="bottom">
			<button class="custom-button" type="default" plain="true" open-type="contact">
				<image src="/static/index/contact.png" mode="widthFix"></image>
			</button>
		</view>
	</view>

</template>

<script>
import carList from "@/components/cart/cart.vue"
import guessLike from "@/components/cart/guessLike.vue"
import {
	getCartList,
	handleCart,
	getUserInfo
} from '@/api/api.js'
export default {
	components: {
		carList,
		guessLike
	},
	data() {
		return {
			keyword: '',
			maskTitle: '',
			carList: [],
			group_id: 0,
			allSel: false
		}
	},
	onShow() {
		this.getCart()
		getUserInfo().then(res => {
			this.group_id = res.data.group_id || 0
		})
	},

	methods: {
		allSelClick(e) {
			this.allSel = e;
		},
		getCart() {
			getCartList({
				keyword: this.keyword
			}).then(res => {
				if (res.code == 1) {

					const array = Object.values(res.data);
					console.log(array);
					console.log(this.carList);

					if (this.carList.length === 0 || this.keyword) {
						// 如果 carList 为空，则将所有 gitem 的 selected 设置为 false
						array.forEach(item => {
							item.glist.forEach(gitem => {
								gitem.selected = false;
							});
						});
						this.carList = array;
					} else {
						// 如果 carList 不为空，则将所有 gitem 的 selected 设置为 false
						array.forEach((item, index) => {
							item.selected = this.carList[index].selected || false;
							item.glist.forEach((gitem, key) => {
								gitem.selected = this.carList[index].glist[key].selected || false;
							});
						});
						// 检查全选状态

						this.$refs.mycar.getAllMount(array);
						this.carList = array;
					}
					let allSelected = true;
					array.forEach(item => {
						if (!item.selected || item.glist.some(gitem => !gitem.selected)) {
							allSelected = false;
						}
					});
					this.allSel = allSelected;



				}

			})

		},

		onChange(e) {

		},
		onSearch(e) {
			this.keyword = e.detail
			this.getCart()
		},
		selGoods: function (carList) {
			var that = this;
			that.carList = carList;
		},
		selShop: function (carList) {
			var that = this;
			that.carList = carList;
		},
		allSelBtn: function (carList) {
			var that = this;
			that.carList = carList;
		},
		jsbtn: function (ids) { //结算按钮
			var that = this;
			console.log(ids);
			var that = this;
			/* 
			 跳转结算页面

			*/
		},
		// delbtn: function (ids, list) {//删除按钮
		// 	var that = this;
		// 	/* 
		// 	 请求接口数据

		// 	*/
		// 	for (let i = 0; i < list.length; i++) {
		// 		for (let k = 0; k < list[i].glist.length; k++) {
		// 			if (list[i].glist[k].selected == true) {
		// 				list[i].glist.splice(k, 1);
		// 				k--
		// 			}
		// 		}
		// 		if (list[i].glist.length == 0) {//若当前店铺商品删除完，删除当前店铺
		// 			list.splice(i, 1);
		// 			i--
		// 		}
		// 	}
		// 	that.carList = list;

		// 	that.$refs.mycar.getAllMount(list);//计算价格展示
		// },
		delThis(id, goods_id) {
			let parms = {
				type: 'del',
				// 'data[sku_id]': id,
				// 'data[goods_id]': goods_id,
				data: [{
					sku_id: id,
					goods_id: goods_id
				}]
			}
			handleCart(parms).then(res => {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (res.code == 1) {
					this.getCart()
				}
			})
			// carList[shopInd
		},
		changeNum(number, id, type, goods_id, shop_id) {
			let parms = {
				type: type == 1 ? 'bcadd' : 'bcsub',
				sku_id: id,
				number: number,
				goods_id: goods_id,
				shop_id: shop_id

			}
			handleCart(parms).then(res => {
				uni.showToast({
					title: res.msg,
					icon: 'none'
				})
				if (res.code == 1) {
					this.getCart()
				}
			})
			this.$refs.mycar.getAllMount(carList); //计算价格展示
		}
	},
	onReachBottom() {

	},
	onShareAppMessage() {

	}
}
</script>


<style scoped lang="scss">
.cart_lis {
	padding-bottom: 150upx;
}

.likeimages {
	width: 500upx;
	margin: 0 auto;

	image {
		width: 500upx;
		height: 42upx;
	}
}

.bottom {
	position: fixed;
	bottom: 10%;
	right: 2%;

	.custom-button {
		border: none !important;
		padding: 0 !important;
		line-height: normal;

		image {
			width: 117rpx;
			height: 117rpx;
			object-fit: cover;
		}
	}
}
</style>